﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;

namespace StructuredProgrammingAssignment.Models.Configurations
{
	public class DynamicQueryCollection : ConfigurationElementCollection
	{
		public DynamicQueryElement this[int index] {
			get {
				return base.BaseGet(index) as DynamicQueryElement;
			}
			set {
				if (base.BaseGet(index) != null) {
					base.BaseRemoveAt(index);
				}
				this.BaseAdd(index, value);
			}
		}

		protected override ConfigurationElement CreateNewElement() {
			return new DynamicQueryElement();
		}

		protected override object GetElementKey(ConfigurationElement element) {
			return ((DynamicQueryElement)element).Key;
		}

		public Dictionary<string, string> ValueDictionary() {
			Dictionary<string, string> result = new Dictionary<string, string>();

			for (int i = 0; i < this.Count; i++) {
				result.Add(this[i].Key, this[i].Value);
			}

			// Init default values
			if (!result.ContainsKey("whereMaxLength")) {
				result.Add("whereMaxLength", "30");
			}
			if (!result.ContainsKey("orderMaxLength")) {
				result.Add("orderMaxLength", "10");
			} 

			return result;
		}
	}
}